package com.yuqih.common.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import com.yuqih.common.service.IIdGenerator;

/**
 * "yyyyMMddHHmmss" + "C" + "2位" + "N" + 3位
 * 
 * @author zhkz
 *
 */
public class String20IdGenerator implements IIdGenerator {
	
	private final AtomicInteger seed = new AtomicInteger(RandomUtils.nextInt());
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	private String appId = "02";
		
	@Override
	public Object generate() {
		String id = sdf.format(new Date());
		id = id + "C" + appId;
		
		long num = Integer.MAX_VALUE + (long) seed.incrementAndGet() + 1;
		String numString = StringUtils.right(String.valueOf(num), 3);
		numString = StringUtils.leftPad(numString, 3, "0");
		
		return id + numString;
	}

	/**
	 * @param appId the appId to set
	 */
	public void setAppId(String appId) {
		this.appId = appId;
	}
	
	

}
